package com.henryschein.mmvvcc.controller;

import java.util.List;

import com.henryschein.mmvvcc.dao.PersonDao;
import com.henryschein.mmvvcc.dao.UserDao;
import com.henryschein.mmvvcc.model.Person;
import com.henryschein.mmvvcc.model.User;
import com.henryschein.mmvvcc.slave.SendMail;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/person/")
public class PersonController {
	
	private static final Logger logger = LoggerFactory.getLogger(PersonController.class);

	@Autowired
	private PersonDao personDao;
        
        @Autowired
	private UserDao userDao;
	
	@RequestMapping(method=RequestMethod.GET,value="edit")
	public ModelAndView editPerson(@RequestParam(value="id",required=false) Long id) {		
		logger.debug("Received request to edit person id : "+id);				
		ModelAndView mav = new ModelAndView();		
 		mav.setViewName("edit");
 		Person person = new Person();
 		mav.addObject("person", person);
		return mav;
	}
        
        @RequestMapping(method=RequestMethod.GET, value="list")
	public ModelAndView listPeople(@RequestParam(value="cat",required=false) String cat) {
		logger.debug("Received request to list persons");
		ModelAndView mav = new ModelAndView();
		List<Person> people;
                if (cat == null){
                   people = personDao.getPeople(); 
                } else {
                    people = personDao.sotrPeople(cat);
                }
		logger.debug("Person Listing count = "+people.size());
		mav.addObject("people",people);
                Person person = new Person();
 		mav.addObject("person", person);
		mav.setViewName("list");
		return mav;
		
	}
        
        @RequestMapping(method=RequestMethod.POST,value="list") 
	public String sortPerson(@ModelAttribute Person person) {
		return "redirect:list?cat="+person.getCategory();
	}
	
	@RequestMapping(method=RequestMethod.POST,value="edit") 
	public String savePerson(@ModelAttribute Person person) {
		logger.debug("Received postback on person "+person);
                person.setVote("0");
		personDao.save(person);
                for (User us : userDao.getUsers()){
                    SendMail.send(us.getMail(), person.getQuote());
                }
                //Add here mail
		return "redirect:list";
	}
        
        @RequestMapping(method=RequestMethod.GET,value="vote")
	public String votePerson(Model model, @RequestParam(value="id",required=false) Long id) {		
		logger.debug("Received request to vote person id : "+id);				
 		Person person = personDao.find(id);
                person.vote();
                personDao.save(person);
		return "redirect:../?id=" + id;
		
	}

}
